package com.xc.string;

import jdk.nashorn.internal.ir.IfNode;

import java.util.HashMap;
import java.util.Map;

public class Pstring {

    /**
     * 反转字符串
     *
     * @param
     * @throws Exception
     */
    public static char[] rs1(String s) {
        char[] chars = s.toCharArray();
        char c[] = new char[chars.length];
        for (int i = 0; i < chars.length; i++) {
            c[chars.length - 1 - i] = chars[i];
        }
        return c;

    }

    public static char[] rs2(String s) {
        char c[] = s.toCharArray();
        int left = 0;
        int right = c.length - 1;
        char tmp = '0';
        while (left < right) {
            tmp = c[left];
            c[left] = c[right];
            c[right] = tmp;
            left++;
            right--;
        }
        return c;

    }

    /**
     * 第一个不重复的字符
     *
     * @param
     * @throws Exception
     */

    public static int one(String s) {
        Map<Character, Integer> fre = new HashMap<>();

        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if (fre.get(chars[i]) != null) {
                fre.put(chars[i], fre.get(chars[i])+1);
            } else {
                fre.put(chars[i], 1);
            }

        }
        for (int i = 0; i < chars.length; i++) {
            if (fre.get(chars[i]) != null&&fre.get(chars[i])  == 1) {
                return i;
            }
        }
        return -1;

    }

    public static void main(String[] args) throws Exception {
        char[] chaoshens = rs2("chaoshen");
        for (int i = 0; i < chaoshens.length; i++) {
//            System.out.println(chaoshens[i]);
//            System.out.println(chaoshens[i]);
        }

        System.out.println(one("aadadaad"));

    }
}
